let winResize = []
let winRefresh = []
let winOnload = []

window.onresize = function() {
	winResize.forEach(fun => fun())
}

window.onload = function() {
	winOnload.forEach(fun => fun())
}

window.addEventListener('beforeunload', () => {
	winRefresh.forEach(fun => fun())
}, false)

function addWinResize(fun) {
	winResize.push(fun)
}

function addWinRefresh(fun) {
	winRefresh.push(fun)
}

function addWinOnload(fun) {
	winOnload.push(fun)
}

/**
 * 判断dom元素是否存在于某个dom节点下面(默认是否存在dom树当中)
 */
function exist(dom, parent = document.body) {
	if (!dom || !parent) {
		return false;
	}
	let parentNode = dom;
	while (parentNode = parentNode.parentNode) {
		if (parent === parentNode) {
			return true;
		}
	}
	return false;
}

const toolWin = {
	addWinResize,
	addWinRefresh,
	addWinOnload,
	exist
}

export default toolWin;
